package com.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blog.constant.MsgConstant;
import com.blog.pojo.dto.NoteDTO;
import com.blog.pojo.entity.Note;
import com.blog.pojo.entity.PageBean;
import com.blog.result.Result;
import com.blog.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/note")
public class NoteController {
    @Autowired
    private NoteService noteService;

    /**
     * 分页查询笔记
     * @param currentPage 当前查询的页码是第几页
     * @param pageSize
     * @param content
     * @return
     */
    @GetMapping("/page")
    public Result<PageBean> getNoteListPage(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String content
    ){
        log.info("开始分页查询笔记：{}, {}, {}", currentPage, pageSize, content);

        try {
            IPage<Note> iPage = noteService.getNoteListPage(currentPage, pageSize, content);
            return Result.success(new PageBean<>(iPage.getTotal(), iPage.getRecords()));
        } catch (Exception e) {
            log.info("分页查询笔记事变：{}", e);
            return Result.error(MsgConstant.NOTE_SELECT_ERROR);
        }
    }

    /**
     * 添加笔记
     * @param noteDTO
     * @return
     */
    @PostMapping("add")
    public Result addNote(@RequestBody NoteDTO noteDTO){
        log.info("开始添加笔记：{}", noteDTO);

        try {
            noteService.addNote(noteDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("添加笔记失败:{}", e);
            return Result.error(MsgConstant.NOTE_ADD_ERROR);
        }
    }

    /**
     * 修改笔记
     * @param noteDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateNote(@RequestBody NoteDTO noteDTO){
        log.info("开始修改笔记：{}", noteDTO);

        try {
            noteService.updateNote(noteDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("修改笔记失败：{}", e);
            return Result.error(MsgConstant.NOTE_UPDATE_ERRPR);
        }
    }

    /**
     * 删除单个笔记
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteNote(@PathVariable Long id){
        log.info("开始删除单个笔记");

        try {
            noteService.deleteNote(id);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("删除单个笔记失败：{}", e);
            return Result.error(MsgConstant.NOTE_DELETE_ERROR);
        }
    }

    /**
     * 批量删除笔记
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteNotes(@RequestBody List<Long> ids){
        log.info("开始批量删除笔记：{}", ids);

        try {
            noteService.deleteNotes(ids);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("批量删除笔记失败：{}", e);
            return Result.error(MsgConstant.NOTE_DELETE_ERROR);
        }
    }

}
